//
//  AddEventViewController.m
//  League
//
//  Created by JinHvung on 2018/2/12.
//  Copyright © 2018年 ciyuanjuzhen. All rights reserved.
//

#import "AddEventViewController.h"

@interface AddEventViewController ()

@property (weak, nonatomic) IBOutlet UILabel *titInfoLab;
@property (weak, nonatomic) IBOutlet UILabel *nameLab;
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UILabel *urlLab;
@property (weak, nonatomic) IBOutlet UITextField *urlField;

@property (weak, nonatomic) IBOutlet UILabel *titDescriptionLab;
@property (weak, nonatomic) IBOutlet UITextView *descriptionTextView;

@property (weak, nonatomic) IBOutlet UILabel *titAddressLab;
@property (weak, nonatomic) IBOutlet UITextView *addressTextView;

@property (weak, nonatomic) IBOutlet UILabel *titTimeLab;
@property (weak, nonatomic) IBOutlet UIButton *startTimeBtn;
@property (weak, nonatomic) IBOutlet UILabel *startTimeLab;
@property (weak, nonatomic) IBOutlet UIButton *endTimeBtn;
@property (weak, nonatomic) IBOutlet UILabel *endTimeLab;


@end

@implementation AddEventViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = BACKGROUND_COLOR_F5;
    self.title = @"Add Event";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonItemClick:)];
    
//    self.nameLab.layer.borderColor = BACKGROUND_COLOR_99.CGColor;
//    self.nameLab.layer.borderWidth = 0.5;
//    self.urlLab.layer.borderColor = BACKGROUND_COLOR_99.CGColor;
//    self.urlLab.layer.borderWidth = 0.5;
//    
//    self.descriptionTextView.layer.borderColor = BACKGROUND_COLOR_99.CGColor;
//    self.descriptionTextView.layer.borderWidth = 0.5;
//    
//    self.addressTextView.layer.borderColor = BACKGROUND_COLOR_99.CGColor;
//    self.addressTextView.layer.borderWidth = 0.5;
//    
//    self.startTimeBtn.layer.borderColor = BACKGROUND_COLOR_99.CGColor;
//    self.startTimeBtn.layer.borderWidth = 0.5;
//    self.endTimeBtn.layer.borderColor = BACKGROUND_COLOR_99.CGColor;
//    self.endTimeBtn.layer.borderWidth = 0.5;
    
    [self.startTimeBtn addTarget:self action:@selector(startTimeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.endTimeBtn addTarget:self action:@selector(endTimeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)startTimeBtnClick:(id)sender {
    BaseDatePickerView *picker = [[BaseDatePickerView alloc] initWithInitialDate:self.startTimeLab.text];
    [self.view addSubview:picker];
    picker.confirmBlock = ^(NSString *selectedDate) {
        self.startTimeLab.text = selectedDate;
    };
}
- (void)endTimeBtnClick:(id)sender {
    BaseDatePickerView *picker = [[BaseDatePickerView alloc] initWithInitialDate:self.endTimeLab.text];
    [self.view addSubview:picker];
    picker.confirmBlock = ^(NSString *selectedDate) {
        self.endTimeLab.text = selectedDate;
    };
}
- (void)rightBarButtonItemClick:(id)sender {
    if(!self.nameField.text || [self.nameField.text isEqual:@""]){
        [SVProgressHUD showInfoWithStatus:@"Please fill in the name"];
        return;
    }
    if(!self.urlField.text || [self.urlField.text isEqual:@""]){
        [SVProgressHUD showInfoWithStatus:@"Please fill in the url"];
        return;
    }
    if(!self.descriptionTextView.text || [self.descriptionTextView.text isEqual:@""]){
        [SVProgressHUD showInfoWithStatus:@"Please fill in the description"];
        return;
    }
    if(!self.addressTextView.text || [self.addressTextView.text isEqual:@""]){
        [SVProgressHUD showInfoWithStatus:@"Please fill in the address"];
        return;
    }
    if(!self.startTimeLab.text || [self.startTimeLab.text isEqual:@""]){
        [SVProgressHUD showInfoWithStatus:@"Please selected the start time"];
        return;
    }
    if(!self.endTimeLab.text || [self.endTimeLab.text isEqual:@""]){
        [SVProgressHUD showInfoWithStatus:@"Please selected the end time"];
        return;
    }
    
    
    NSDictionary *dic = @{
                          @"groupId": self.groupId,
                          @"customerId": [UserInfomation shareManager].pk,
                          @"eventName": self.nameField.text,
                          @"url": self.urlField.text,
                          @"description": self.descriptionTextView.text,
                          @"address": self.addressTextView.text,
                          @"startTime": self.startTimeLab.text,
                          @"endTime": self.endTimeLab.text
                          };
    NSString *apiStr = @"/event/create";
    NSLog(@"%@-params:%@", apiStr, dic);
    NSString *urlStr = [NSString stringWithFormat:@"%@%@", kFirstUrl, apiStr];
    [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    [DataContrller PostRequestUrl:urlStr Param:dic requestSuccess:^(NSDictionary *backDic) {
        [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
        
        NSLog(@"%@-backDic:%@", apiStr, backDic);
        if ([backDic[@"returnCode"] integerValue] == 200) {
            [SVProgressHUD showSuccessWithStatus:@"Add Success"];
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [SVProgressHUD showErrorWithStatus:backDic[@"msg"]];
        }
    } requestFail:^(NSString *backErrorStr) {
        [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
        NSLog(@"%@-backErrorStr:%@", apiStr, backErrorStr);
        
    }];
}

@end
